package cn.itcast.bos.service.base.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.StandardRepository;
import cn.itcast.bos.domain.base.Standard;
import cn.itcast.bos.service.base.StandardService;

@Service
@Transactional
public class StandardServiceImpl implements StandardService {

	// 注入DAO
	@Autowired
	private StandardRepository standardRepository;

	/**
	 * 用到了spring 的cache缓存技术
	 */
	@Override
	@CacheEvict(value="standard",allEntries=true)  //对缓存区清除缓存
	public void save(Standard standard) {
		standardRepository.save(standard);
	}

	/**
	 * 因为查询分页有参数，所以参数不变的情况下可以根据缓存记录显示
	 * 根据spring手册可以根据不变的参数设置关键字，此处用到了spel表达式
	 */
	@Override
	@Cacheable(value="standard",key="#pageable.pageNumber+'_'+#pageable.pageSize")
	public Page<Standard> findPageData(Pageable pageable) {
		return standardRepository.findAll(pageable);
	}

	/**
	 * 用到了spring 的cache缓存技术
	 */
	@Override
	@Cacheable("standard")   //设置缓存，保存到缓存区standard
	public List<Standard> findAll() {
		return standardRepository.findAll();
	}

}
